package cn.jee.controller;

import cn.jee.entity.Advisor;
import cn.jee.entity.Mentee;
import cn.jee.repository.AdvisorRepository;
import cn.jee.repository.MenteeRepository;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/")
@AllArgsConstructor
public class AdvisorActions {
  private  final MenteeRepository menteeRepository;
  private  final AdvisorRepository advisorRepository;
  @GetMapping("/")
  public String showAdvisorList(Model model) {
    List<Advisor> advisors = advisorRepository.findAll(Sort.by(Sort.Direction.ASC, "fullName"));
    model.addAttribute("advisors", advisors);
    return "advisor_list";
  }
  @PostMapping("/advisor/save")
  public String saveAdvisor(@RequestParam String fullName, @RequestParam String researchField, Model model) {
    if (fullName == null || fullName.trim().isEmpty() || researchField == null || researchField.trim().isEmpty()) {
      model.addAttribute("error", "教师姓名和研究方向不能为空");
      return "advisor_form";
    }
    Advisor advisor = new Advisor();
    advisor.setFullName(fullName);
    advisor.setResearchField(researchField);
    advisorRepository.save(advisor);
    return "redirect:/";
  }

  @GetMapping("/advisor/create")
  public String showAdvisorForm() {
    return "advisor_form";
  }
  @GetMapping("/assignMentees")
  public String assignMentees(@RequestParam Long advisorId, Model model) {
    Advisor advisor = advisorRepository.findById(advisorId).orElse(null);
    if (advisor == null) return "redirect:/";
    List<Mentee> unassigned = menteeRepository.findByAdvisorIsNull();
    model.addAttribute("advisor", advisor);
    model.addAttribute("unassigned", unassigned);
    return "assign_page";
  }

  @GetMapping("/assignOne")
  public String assignOneMentee(@RequestParam Long menteeId, @RequestParam Long advisorId) {
    Mentee mentee = menteeRepository.findById(menteeId).orElse(null);
    Advisor advisor = advisorRepository.findById(advisorId).orElse(null);
    if (mentee != null && advisor != null) {
      mentee.setAdvisor(advisor);
      menteeRepository.save(mentee);
    }
    return "redirect:/assignMentees?advisorId=" + advisorId;
  }
  @GetMapping("/uploadAvatarPage")
  public String showUploadPage(@RequestParam Long menteeId, @RequestParam Long advisorId, Model model) {
    model.addAttribute("menteeId", menteeId);
    model.addAttribute("advisorId", advisorId);
    return "upload_form";
  }
  @PostMapping("/uploadAvatar")
  public String uploadAvatar(@RequestParam Long menteeId,
                             @RequestParam Long advisorId,
                             @RequestParam("file") MultipartFile file) {
    Mentee mentee = menteeRepository.findById(menteeId).orElse(null);
    if (mentee != null && !file.isEmpty()) {
      try {
        String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        File dest = new File("d:/" + filename);
        file.transferTo(dest);
        mentee.setAvatarFileName(filename);
        menteeRepository.save(mentee);
      } catch (IOException e) {
        throw new RuntimeException("文件上传失败：" + e.getMessage());
      }
    }
    return "redirect:/assignMentees?advisorId=" + advisorId;
  }



}
